var orderFactory = {
		cookie : function(){
			var cook = document.cookie.split(";");
			var userId;
			cook.map(function(c){
				if(c.indexOf('userId') != -1){
					userId = c.split("=")[1];
				}
			});
			if(userId) return userId;
		},
		extend : function(dist, source){
			for(var i in source) if(source[i]) dist[i] = source[i];
		},
		addScroll : function(){
			var timer = null;
			$(window).scroll(function(e){
				
				if(timer) clearTimeout(timer);
				timer = setTimeout(function(){
					var wh = document.body.clientHeight + document.body.scrollTop - $("#goOrder").height();
					var fh = $("#footer").offset().top;
					if(wh > fh){
						$("#goOrder").css({'top':fh+"px",position:"absolute"});
					}else{
						$("#goOrder").css({'top':"auto",position:"fixed"});
					}

				},50);
			});
		},
		selectItemFn : function(name){
			var self = this;
			return function(item,e){
				var checked = e.target.checked;
				if(!self[name]) self[name] = {};
				if(checked)
					self[name][item.id] = item;
				else
					delete self[name][item.id];
				self.setState({selectItem : self[name]});
				self.state[name] = self[name];
				if(name === 'selectOrderType') this.searchBySelect('','orderType');
				else self.fresh();
				
			}
		},
		fresh : function(){
			this.setState({frest:!this.state.fresh});
		},
		getSelectedItem : function(name){
			var items = [];
			for(var i in this[name]){
				items.push(i);
			}
			return items;
		},
		search : function(pageIndex,pageSize){
			var input = this.refs.sinput;
			// if(!input.value){
			// 	input.focus();
			// 	return;
			// }
			var params = {q:input.value,supplierId:this.state.purchaseId};
			if(pageIndex && typeof pageIndex != 'object'){
				params.pageIndex = pageIndex;
			}
			if(pageSize && typeof pageSize != 'object'){
				params.pageSize = pageSize;
			}
			
			var orderTypes = this.getSelectedItem("selectOrderType");
			if(orderTypes.length > 0){
				params.goodsTypeId = orderTypes.join(",");
			}
			console.log(params,'params')
			trdOrdersListService.goodList(params,this.handleList);
		},
		handleList : function(res){
			if(res.errorCode == 0){
				this.setState({list:res.data,total : res.pagination.totalResult});
			}
		},
		searchBySelect : function(id){
			var orderTypes;
				orderTypes = this.getSelectedItem("selectOrderType");
			var params = {};

			if(id){
				var list = this.state.selectOrderList;
				for(var i in list) delete list[i];
				params.supplierId = id;
				this.setState({purchaseId:id});
			}else{
				params.supplierId = this.state.purchaseId;
			}
			if(orderTypes.length > 0){
				params.goodsTypeId = orderTypes.join(",");
			}
			var text = this.refs.sinput.value;
			if(text) params.q = text;

			trdOrdersListService.goodList(params, this.handleList);
		},
		buy : function() {
			var selectedItem = this.getSelectedItem('selectOrderList');
			if(selectedItem.length === 0) {
				PromptComponent.init({'type':'warning','msg':"请先选择商品!"}).show();
				return;
			}

			var purchaseArr = this.state.purchase,purchaseId = this.state.purchaseId,purchaseName = '';

			purchaseArr.forEach(function(item){
				if(item.id === purchaseId) purchaseName = item.name;
			});

			window.location = '/trdOrder?ids=' + selectedItem.join(",") + "&purchaseId=" + purchaseId + '&purchaseName=' + purchaseName + "&relate=trdOrdersList";
		},
		onPageChange : function(pageIndex,pageSize){
			this.search(pageIndex,pageSize);
		}
		
	};

	module.exports = orderFactory;

